package com.baiyun.controller.admin;


import com.baiyun.Result.Result;
import com.baiyun.entity.User;
import com.baiyun.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/admin/user")
@RequiredArgsConstructor
@Api(tags = "管理员端——用户管理")
public class UserController {

    private final UserService userService;

    /**
     * 查询所有用户
     * @return
     */
    @GetMapping
    @ApiOperation(value="查询所有用户")
    public Result<List<User>> selectAllUser() {
        log.info("查询所有用户");
        List<User> userList=userService.selectAllUser();
        return Result.success(userList);
    }

    /**
     * 新增用户
     * @param user
     * @return
     */
    @PostMapping()
    @ApiOperation(value="新增用户")
    public Result addUser(@RequestBody User user){
        log.info("新增用户");
        userService.addUser(user);
        return Result.success();
    }

    /**
     * 修改用户
     * @param user
     * @return
     */
    @PutMapping()
    @ApiOperation(value="修改用户")
    public Result updateUser(User user){
        log.info("修改用户");
        userService.updateUser(user);
        return Result.success();
    }

    /**
     * 删除用户
     * @param uid
     * @return
     */
    @DeleteMapping("/{uid}")
    @ApiOperation(value="删除用户")
    public Result deleteUser(@ApiParam("用户id") @PathVariable("uid") Integer uid){
        log.info("删除id为{}用户",uid);
        userService.deleteUser(uid);
        return Result.success();
    }

    /**
     * 查询用户通过id
     * @return
     */
    @GetMapping("/{uid}")
    @ApiOperation(value="根据id查询用户")
    public Result<User> selectUserById(@ApiParam("用户id")@PathVariable Integer uid){
        log.info("查询用户");
        return Result.success(userService.selectUserById(uid));
    }

    /**
     * 根据用户名模糊查询用户
     * @param username
     * @return
     */
    @GetMapping("/search/{username}")
    @ApiOperation(value="根据用户名模糊查询用户")
    public Result<List<User>> selectByName(@ApiParam("用户名")@PathVariable("username") String username){
        log.info("根据用户名模糊查询用户");
        return Result.success(userService.selectByName(username));
    }

}
